﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using Telerik.Web.UI;
using System.Data;
using SRM;

public partial class UserControls_RecreationUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void HobbyCategory_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Telerik.Web.UI.RadComboBox hobbycategory_combobox = null;
        Telerik.Web.UI.RadComboBox hobbydescription_combobox = null;
        System.Web.UI.WebControls.TextBox hobbyothers_textbox = null;

        hobbycategory_combobox = sender as Telerik.Web.UI.RadComboBox;
        hobbydescription_combobox = hobbycategory_combobox.Parent.FindControl("hobbydescription_combobox") as Telerik.Web.UI.RadComboBox;
        hobbyothers_textbox = hobbycategory_combobox.Parent.FindControl("hobbyothers_textbox") as System.Web.UI.WebControls.TextBox;

        hobbydescription_combobox.Items.Clear();
        hobbydescription_combobox.Items.Add(new RadComboBoxItem("Please select", string.Empty));
        hobbydescription_combobox.Text = "";

        hobbydescription_combobox.DataTextField = "DisplayText";
        hobbydescription_combobox.DataValueField = "Id";
        hobbydescription_combobox.DataSource = SRM_Code.GetHobbyDescription("1", hobbycategory_combobox.SelectedValue);
        hobbydescription_combobox.DataBind();

        // find others row
        System.Web.UI.HtmlControls.HtmlTableRow recreation_other_row = hobbycategory_combobox.Parent.FindControl("recreation_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;
        System.Web.UI.HtmlControls.HtmlTableRow recreation_description_row = hobbycategory_combobox.Parent.FindControl("recreation_description_row") as System.Web.UI.HtmlControls.HtmlTableRow;

        if ((recreation_other_row != null) && (recreation_description_row != null))
        {
            if (hobbycategory_combobox.SelectedValue == "999")
            {
                recreation_description_row.Visible = false;
                recreation_other_row.Visible = true;
                hobbyothers_textbox.Text = string.Empty;
            }
            else
            {
                recreation_description_row.Visible = true;
                recreation_other_row.Visible = false;
                hobbyothers_textbox.Text = string.Empty;
            }
        }
    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Recreation_ObjectDataSource.UpdateParameters["hobbyCategory"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Recreation_ObjectDataSource.InsertParameters["hobbyCategory"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgRecreation_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Recreation_ObjectDataSource.UpdateParameters["recreationKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("recreationKey"));
            Recreation_ObjectDataSource.UpdateParameters["hobbyCategory"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("hobbycategory_combobox"));
            Recreation_ObjectDataSource.UpdateParameters["hobbyDescription"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("hobbydescription_combobox"));
            Recreation_ObjectDataSource.UpdateParameters["hobbyOthers"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("hobbyothers_textbox"));

            if (ValidateFormFields("UPDATE"))
            {
                Recreation_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("RecreationMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            Recreation_ObjectDataSource.DeleteParameters["recreationKey"].DefaultValue = item["RecreationKey_column"].Text;
            Recreation_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            Recreation_ObjectDataSource.InsertParameters["hobbyCategory"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("hobbycategory_combobox"));
            Recreation_ObjectDataSource.InsertParameters["hobbyDescription"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("hobbydescription_combobox"));
            Recreation_ObjectDataSource.InsertParameters["hobbyOthers"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("hobbyothers_textbox"));
            
            if (ValidateFormFields("INSERT"))
            {
                Recreation_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("RecreationMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }
    }

    protected void rgRecreation_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DataTable dt = null;
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            Telerik.Web.UI.RadComboBox hobbycategory_combobox = null;
            Telerik.Web.UI.RadComboBox hobbydescription_combobox = null;
            System.Web.UI.WebControls.TextBox hobbyothers_textbox = null;

            hobbycategory_combobox = geiEditedItem.FindControl("hobbycategory_combobox") as Telerik.Web.UI.RadComboBox;
            hobbydescription_combobox = geiEditedItem.FindControl("hobbydescription_combobox") as Telerik.Web.UI.RadComboBox;
            hobbyothers_textbox = geiEditedItem.FindControl("hobbyothers_textbox") as System.Web.UI.WebControls.TextBox;

            // Databind the 3 combo-boxes, hobbycategory_combobox
            Help.BindSapCodeToComboBox(hobbycategory_combobox, SRM_Code.GetHobbyCategory("1"));
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                //hobbycategory_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["HobbyCategory"].ToString();
                Help.SetComboBoxSelectedValue(hobbycategory_combobox, ((DataRowView)e.Item.DataItem).Row["HobbyCategory"].ToString());
            }

            Help.BindSapCodeToComboBox(hobbydescription_combobox, SRM_Code.GetHobbyDescription("1", hobbycategory_combobox.SelectedValue));
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                //hobbydescription_combobox.SelectedValue = ((System.Data.DataRowView)e.Item.DataItem).Row["HobbyDescription"].ToString();
                Help.SetComboBoxSelectedValue(hobbydescription_combobox, ((DataRowView)e.Item.DataItem).Row["HobbyDescription"].ToString());
            }

            // find others row
            System.Web.UI.HtmlControls.HtmlTableRow recreation_other_row = hobbycategory_combobox.Parent.FindControl("recreation_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;
            System.Web.UI.HtmlControls.HtmlTableRow recreation_description_row = hobbycategory_combobox.Parent.FindControl("recreation_description_row") as System.Web.UI.HtmlControls.HtmlTableRow;

            if ((recreation_other_row != null) && (recreation_description_row != null))
            {
                if (hobbycategory_combobox.SelectedValue == "999")
                {
                    recreation_description_row.Visible = false;
                    recreation_other_row.Visible = true;
                    hobbyothers_textbox.Text = ((System.Data.DataRowView)e.Item.DataItem).Row["HobbyOthers"].ToString();
                }
                else
                {
                    recreation_description_row.Visible = true;
                    recreation_other_row.Visible = false;
                    hobbyothers_textbox.Text = string.Empty;
                }
            }

        } // if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    }
}